package handler

import (
	"gitee.com/simple-set/simple.io/src/process/socks"
	"gitee.com/simple-set/simple.proxy/src/config"
)

type SocksAuth struct {
	authUser *AuthUser
}

// NewSocksAuth 认证器构造函数
func NewSocksAuth() *SocksAuth {
	socksAuth := &SocksAuth{authUser: NewAuthUser()}
	if config.Config.Socks.AuthFilePath != "" {
		socksAuth.authUser.LoadUser(config.Config.Socks.AuthFilePath)
	}
	if config.Config.Socks.AuthUser != "" && config.Config.Socks.AuthPassword != "" {
		socksAuth.authUser.AddUser(config.Config.Socks.AuthUser, config.Config.Socks.AuthPassword)
	}
	return socksAuth
}

func (a SocksAuth) AuthMethod() socks.AuthType {
	return socks.UserPassAuth
}

func (a SocksAuth) Challenge(user, pass string) bool {
	return a.authUser.Challenge(user, pass)
}

func SocksAuthEnable() bool {
	return config.Config.Socks.AuthFilePath != "" || config.Config.Socks.AuthUser != ""
}
